package jiubeirobot.com.robotshop.ui.fragment.shop;

import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.Activitys;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.base.BaseData;
import jiubeirobot.com.robotshop.base.BaseFragment;
import jiubeirobot.com.robotshop.bean.ShopDetailsBean;
import jiubeirobot.com.robotshop.bean.StockBean;
import jiubeirobot.com.robotshop.common.view.BannerLayout;
import jiubeirobot.com.robotshop.common.view.CustomNumInputView;
import jiubeirobot.com.robotshop.data.constans.Constants;
import jiubeirobot.com.robotshop.data.net.APIManager;
import jiubeirobot.com.robotshop.ui.activity.shop.ShopBuyListActivity;
import jiubeirobot.com.robotshop.utils.DataUtil;
import jiubeirobot.com.robotshop.utils.L;
import jiubeirobot.com.robotshop.utils.TS;

/**
 * ==========================
 * 创建人：TheJoker丶豪 on
 * 创建时间：2017/8/22.
 * 创建内容：首页显示商品列表的 详情fragment
 * ==========================
 */

public class ShopDetailsFragment extends BaseFragment {
    @BindView(R.id.iv_close)
    ImageView ivclose;
    @BindView(R.id.rl_card)
    RelativeLayout rlcard;
    @BindView(R.id.tv_shop_number)
    TextView tvshopnumber;
    @BindView(R.id.sv_details)
    ScrollView svdetails;
    @BindView(R.id.bl_shop_banner)
    BannerLayout blshop;
    @BindView(R.id.btn_add_to_card)
    Button btnaddshop;
    @BindView(R.id.btn_buy)
    Button btnbuy;
    @BindView(R.id.cniv_num)
    CustomNumInputView cnivNum;


    private ShopBuyListActivity shopBuyListActivity;
    private String id;
    private String goods_id;
    private String delivery_id;
    private ShopDetailsBean sDetailsBean;


    @Override
    public int pageLayout() {
        id = getArguments().getString(Constants.INTENT_SHOP_ID);
        return R.layout.fragment_shop_details;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public void initTitle() {
        shopBuyListActivity = (ShopBuyListActivity) getActivity();
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    }

    @Override
    public void onResume() {
        super.onResume();
        APIManager.getInstance().shopDetails(getContext(), this, "6", "4");
    }

    @Override
    public void initView() {

    }

    @OnClick({R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                shopBuyListActivity.closeShopDetails();
                break;
            default:
                break;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {

    }


    @OnClick({R.id.btn_buy, R.id.btn_add_to_card, R.id.rl_card})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_buy:
                if (sDetailsBean == null) {
                    return;
                }
                if (Integer.parseInt(sDetailsBean.getGoods_inventory()) == 0) {
                    TS.showToCenter(getString(R.string.common_inventory));
                    return;
                }
                final StockBean stockBean = new StockBean();
                stockBean.setGoods_id(sDetailsBean.getGoods_id());
                stockBean.setGoods_img(sDetailsBean.getGoods_photos()!=null?sDetailsBean.getGoods_photos().get(0):null);
                stockBean.setGoods_current_price(sDetailsBean.getGoods_current_price());

                stockBean.setGoods_inventory(sDetailsBean.getGoods_inventory());
                stockBean.setGoods_name(sDetailsBean.getGoods_name());


                stockBean.setNum(cnivNum.getNum());

                if (getActivity() != null) {
                    //((ShopListActivity) getActivity()).closeShopDetails();
                }
                Activitys.toCheckOrder(getActivity(), stockBean);
                this.onDestroy();

                break;
            case R.id.btn_add_to_card:
                if (sDetailsBean == null) {
                    return;
                }
                if (Integer.parseInt(sDetailsBean.getGoods_inventory()) == 0) {
                    TS.showToCenter(getString(R.string.common_inventory));
                    return;
                }

                if (getActivity() != null && cnivNum.getCurentVal() > 0) {
                    int[] startLocation = new int[2];// 一个整型数组，用来存储按钮的在屏幕的X、Y坐标
                    view.getLocationInWindow(startLocation);// 这是获取购买按钮的在屏幕的X、Y坐标（这也是动画开始的坐标）
                    ImageView ball = new ImageView(getContext());
                    ball.setImageResource(R.mipmap.card_anim_icon);
                    L.i("startLocation：" + startLocation[0] + "    " + startLocation[1]);
                    StockBean sbean = new StockBean();
                    sbean.setGoods_id(sDetailsBean.getGoods_id());
                    sbean.setGoods_img(sDetailsBean.getGoods_photos()!=null?sDetailsBean.getGoods_photos().get(0):null);
                    sbean.setGoods_current_price(sDetailsBean.getGoods_current_price());
                    sbean.setGoods_inventory(sDetailsBean.getGoods_inventory() + "");
                    //TODO  加入购物车 需要判断
                    ((ShopBuyListActivity) getActivity()).addToCard(ball, startLocation, sbean, cnivNum.getNum());
                }
                break;
            case R.id.rl_card:
                if (BaseData.getBaseData().getCardShops().size() > 0) {
                    Activitys.toShoppingCard(getActivity());
                } else {
                    TS.showToCenter(getString(R.string.common_cart_is_null));
                }

                break;
            default:
                break;
        }
    }


    public void setCard() {
//        if (tvShopNumber == null) {
//            return;
//        }
//        int size = BaseData.getBaseData().getCardNums();
//        if (size == 0) {
//            tvShopNumber.setVisibility(View.INVISIBLE);
//        } else if (size > 99) {
//            tvShopNumber.setVisibility(View.VISIBLE);
//            tvShopNumber.setText("99");
//        } else {
//            tvShopNumber.setVisibility(View.VISIBLE);
//            tvShopNumber.setText(size + "");
//        }
    }


}
